IF COL_LENGTH('[dbo].[LSInsightAudit]', 'MinTimestamp') IS NULL
BEGIN
    ALTER TABLE [dbo].[LSInsightAudit]
    ADD [MinTimestamp] BIGINT NULL
END


/****** Object:  StoredProcedure [dbo].[UpdateLSInsightAudit]    Script Date: 23.11.2021 13:25:25 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:      Kristinn Thor Sigurjonsson
-- Create Date: 2019-09-23
-- Description: Insert script to set the latest timestamp for incremental load
-- =============================================
ALTER PROCEDURE [dbo].[UpdateLSInsightAudit]
(
    @ADFRunGUID Nvarchar(100),
    @PipelineName Nvarchar(100),
	@Table Nvarchar(100),
	@CompanyPrefix Nvarchar(100) = NULL,
	@MinTimestamp bigint = NULL,
	@RowVersion bigint,
	@RowsRead int,
	@RowsCopied int, 
	@CopyDuration int,
	@PipelineStart datetime,
	@PipelineTriggerType Nvarchar(100),
	@ErrorMessage Nvarchar(max),
	@Note NVarchar(250) = NULL

	
)
AS
BEGIN
SET NOCOUNT ON

INSERT INTO [dbo].[LSInsightAudit] ([ADF Run GUID], [ADF Pipeline Name], [Table], [CompanyPrefix], [MinTimestamp], [NewRowVersion], [RowsRead], [RowsCopied], [CopyDuration], [PipelineStart], [PipelineFinish], [Pipeline Trigger Type], [ErrorMessage], [Note])
	VALUES (@ADFRunGUID, @PipelineName, @Table, @CompanyPrefix, @MinTimestamp, @RowVersion, @RowsRead, @RowsCopied, @CopyDuration, @PipelineStart, GETDATE(), @PipelineTriggerType, @ErrorMessage, @Note)

END
GO

